package  
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import JSON;
	/**
	 * ...
	 * @author yaempechenki@gmail.com
	 */
	public class SettingsHolder extends EventDispatcher 
	{
		
		/** settings */
		public static var DEVICE_ID:String = "super_16";
		public static var SERVER_URL:String = "http://172.18.68.18/";
		public static var OFFLINE_MODE:Boolean = false;
		public static var FOR_PVE:Boolean = false;
		public static var EDITOR_MODE:Boolean = false;
		public static var EDITOR_MODE_FOR_PVE:Boolean = false;
		public static var DRAW_LAND_TILES:Boolean = false;
		public static var PPI:int = 260;
		public static var WIDTH:int = 1280;
		public static var HEIGHT:int = 768;	
		
		
		private var fileOpenHelper:FileOpenHelper;
		public function SettingsHolder() 
		{
			fileOpenHelper = new FileOpenHelper();
			fileOpenHelper.startBrowse();
			fileOpenHelper.addEventListener(Event.OPEN, onFileOpen);
		}
		
		private function onFileOpen(event:Event):void {
			var obj:Object = JSON.parse(fileOpenHelper.getData());
			SettingsHolder.SERVER_URL = obj["SERVER_URL"];
			SettingsHolder.DEVICE_ID =  obj["DEVICE_ID"];
			SettingsHolder.OFFLINE_MODE =  obj["OFFLINE_MODE"];
			SettingsHolder.FOR_PVE =  obj["FOR_PVE"];
			SettingsHolder.EDITOR_MODE =  obj["EDITOR_MODE"];
			SettingsHolder.EDITOR_MODE_FOR_PVE =  obj["EDITOR_MODE_FOR_PVE"];
			SettingsHolder.DRAW_LAND_TILES =  obj["DRAW_LAND_TILES"];
			SettingsHolder.PPI =  obj["PPI"];
			SettingsHolder.WIDTH =  obj["WIDTH"];
			SettingsHolder.HEIGHT =  obj["HEIGHT"];
			dispatchEvent(new Event(Event.OPEN));
		}
		
		public function save():void {
			const newLine:String = "\n";
			var result:String = '{' + newLine;
			result += '\t"SERVER_URL":"'  + SERVER_URL + '",' + newLine;
			result += '\t"DEVICE_ID":"'  + DEVICE_ID + '",' + newLine + newLine;
			result += '\t"OFFLINE_MODE":'  + OFFLINE_MODE + ',' + newLine;
			result += '\t"FOR_PVE":'  + FOR_PVE + ',' + newLine;
			result += '\t"EDITOR_MODE":'  + EDITOR_MODE + ',' + newLine;
			result += '\t"EDITOR_MODE_FOR_PVE":'  + EDITOR_MODE_FOR_PVE + ',' + newLine;
			result += '\t"DRAW_LAND_TILES":'  + DRAW_LAND_TILES + ',' + newLine + newLine;
			result += '\t"PPI":"'  + PPI + '",' + newLine;
			result += '\t"WIDTH":"'  + WIDTH + '",' + newLine;
			result += '\t"HEIGHT":"'  + HEIGHT + '"' + newLine;
			result += '}';
			fileOpenHelper.save(result);
		}
		
	}

}